//
//  SetViewController.m
//  Travel assistant
//
//  Created by 王京 on 16/3/11.
//  Copyright © 2016年 旅游助手. All rights reserved.
//

#import "SetViewController.h"
#import "DataHelper.h"
#import "MyViewController.h"

@interface SetViewController ()

@property (nonatomic,strong) UIButton *exitBtn;

@property (nonatomic,strong) UIButton *resetImageAndName;


@end

@implementation SetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.resetImageAndName = [UIButton buttonWithType:(UIButtonTypeSystem)];
    
    self.resetImageAndName.frame = CGRectMake(80, self.view.frame.size.height*0.2, 150, 40);
    
    [self.resetImageAndName setTitle:@"清除缓存" forState:(UIControlStateNormal)];
    
    [self.view addSubview:self.resetImageAndName];
    
    [self.resetImageAndName addTarget:self action:@selector(resetImageAndNameAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    if (HELP.loginToView == YES) {
        
    self.exitBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    
    self.exitBtn.frame = CGRectMake(80, self.view.frame.size.height*0.4, 150, 40);
    
    [self.exitBtn setTitle:@"退出登陆" forState:(UIControlStateNormal)];
    
    [self.exitBtn addTarget:self action:@selector(exitBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    
        HELP.loginToView = YES;
    [self.view addSubview:self.exitBtn];
        
    }
    
    if (HELP.registToView == YES) {
        
        self.exitBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];
        
        self.exitBtn.frame = CGRectMake(80, self.view.frame.size.height*0.4, 150, 40);
        
        [self.exitBtn setTitle:@"退出登陆" forState:(UIControlStateNormal)];
        
        [self.exitBtn addTarget:self action:@selector(exitBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
        
        HELP.registToView = YES;
        [self.view addSubview:self.exitBtn];
        
    }
    
    
    // Do any additional setup after loading the2 view.
}

- (void)exitBtnAction{
    MyViewController *my = [[MyViewController alloc]init];
    
    HELP.loginToView = NO;
    
    HELP.registToView = NO;
    
    [self.navigationController pushViewController:my animated:YES];
    
    
}

- (void)resetImageAndNameAction{
    [self clearCacheSuccess];
    
}

-(void)clearCacheSuccess

{
    //定义变量存储总的缓存大小
    long long sumSize = 0;
    
    //01.获取当前图片缓存路径
    NSString *cacheFilePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
    NSLog(@"~~~~~~~~~~~~~~~~~~~~%@", cacheFilePath);
    //02.创建文件管理对象
    NSFileManager *filemanager = [NSFileManager defaultManager];
    
    //获取当前缓存路径下的所有子路径
    NSArray *subPaths = [filemanager subpathsOfDirectoryAtPath:cacheFilePath error:nil];
    
    //遍历所有子文件
    for (NSString *subPath in subPaths) {
        //1）.拼接完整路径
        NSString *filePath = [cacheFilePath stringByAppendingFormat:@"/%@",subPath];
        //2）.计算文件的大小
        long long fileSize = [[filemanager attributesOfItemAtPath:filePath error:nil]fileSize];
        //3）.加载到文件的大小
        sumSize += fileSize;
    }
    float size_m = sumSize/(1024*1024);
    
    
    
    NSString *message = [NSString stringWithFormat:@"%.2fM缓存",size_m];
    
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"清除缓存"
                                                                   message:message
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {
                                                          
                                                              dispatch_async(
                                                                             
                                                                             dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                                                                             
                                                                             , ^{
                                                                                 
                                                                                 NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                                                                                 
                                                                                 NSLog(@"%@", cachPath);
                                                                                 
                                                                                 
                                                                                 
                                                                                 NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                                                                                 
                                                                                 NSLog(@"~~~~~~~~~~~~~~~~~files :%lu",(unsigned long)[files count]);
                                                                                 
                                                                                 for (NSString *p in files) {
                                                                                     
                                                                                     NSError *error;
                                                                                     
                                                                                     NSString *path = [cachPath stringByAppendingPathComponent:p];
                                                                                     
                                                                                     if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                                                                                         
                                                                                         [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                                                                                         
                                                                                     }
                                                                                     
                                                                                 }
                                                                                 });
                                                              
                                                              
                                                              
                                                          }];
    
    [alert addAction:defaultAction];
    [self presentViewController:alert animated:YES completion:nil];
    NSLog(@"清理成功");
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
